package com.eternal.operationLog;

import com.eternal.operationLog.entity.UserLog;
import com.eternal.util.user.service.UserUtil;
import com.eternal.util.user.vo.UserVo;
import lombok.extern.java.Log;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;

import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 用户操作日志记录
 *
 * @Class LogAnnotationAnalytic
 * @Description: TODO
 * @Author Eternal
 * @Version
 * @date 2018/11/15 10:15
 */
@Aspect
@Component
@Log
public class LogAnnotationAnalytic {
    @After("execution(* com..*.*(..)) && @annotation(logAnnotation)")
    public void logAnnotation(final JoinPoint joinPoint, LogAnnotation logAnnotation) {
        try {
            Object[] args = joinPoint.getArgs();
            StringBuilder parameters = new StringBuilder();
            for (int i = 0; i < args.length; i++) {
                parameters.append(args[i] + ",");
            }
            UserVo userVo = UserUtil.getUser();
            UserLog userLog = new UserLog();
            userLog.setApiName(logAnnotation.apiName());
            userLog.setApiValue(logAnnotation.value());
            userLog.setMethod(logAnnotation.method());
            userLog.setUserId(userVo.getUserId());
            userLog.setUserName(userVo.getUserName());
            userLog.setParameters(parameters.toString());
            userLog.setLogDate(new Date());
            userLog.insert();
            log.info(userVo.getUserId() + "===" + userVo.getUserName() + "===" + logAnnotation.apiName());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
